/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package db.JDBC;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.Connection;



/**
 *
 * @author Peter
 */
public class JdbcDaoFactory {


    private final static String JNDI_DATABASE_RESOURCE = "java:comp/env/jdbc/db";
    private static JdbcDaoFactory instance = null;
    private static Connection con= null;
    private static DataSource ds = null;

    /**
     * 
     */
    private JdbcDaoFactory()
    {
        try
        {
            Context cntx = new InitialContext();
            ds = (DataSource) cntx.lookup(JNDI_DATABASE_RESOURCE);
            con = ds.getConnection();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 
     * @param con
     */
    private JdbcDaoFactory(Connection con)
    {
        this.con = con;
    }

    /**
     * 
     * @return
     */
    public static JdbcDaoFactory getInstance()
    {
        if(instance == null)
            instance = new JdbcDaoFactory();        
        return instance;
    }

    /**
     * 
     * @param con
     * @return
     */
    public static JdbcDaoFactory getInstance(Connection con)
    {
        if(instance == null)
            instance = new JdbcDaoFactory(con);
        return instance;
    }
    /**
     * 
     * @return
     */
    public BookJdbcDAO getBookJdbcDao()
    {
        return new BookJdbcDAO(con);
    }

    /**
     * 
     * @return
     */
    public CourseJdbcDAO getCourseJdbcDao()
    {
        return new CourseJdbcDAO(con);
    }

    /**
     * 
     * @return
     */
    public ProgramJdbcDAO getProgramJdbcDao()
    {
        return new ProgramJdbcDAO(con);
    }

    /**
     * 
     * @return
     */
    public SchoolJdbcDAO getSchoolJdbcDAO()
    {
        return new SchoolJdbcDAO(con);
    }

    /**
     * 
     * @return
     */
    public ShopJdbcDAO getShopJdbcDAO()
    {
        return new ShopJdbcDAO(con);
    }

    /**
     * 
     * @return
     */
    public UserJdbcDAO getUserJdbcDAO()
    {
        return new UserJdbcDAO(con);
    }
}
